package com.kede.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.kede.app.domain.SuppliesStore;
import com.kede.app.domain.ZhiUserCertified;
import com.kede.app.repository.SuppliesStoreRepository;
import com.kede.app.repository.ZhiUserCertifiedRepository;
import com.kede.app.service.ZhiUserCertifiedService;
import com.kede.app.service.dto.ZhiUserCertifiedDto;
import com.kede.app.service.dto.ZhiUserCertifiedQueryCriteria;
import com.kede.app.service.mapstruct.ZhiUserCertifiedMapper;
import com.kede.utils.FileUtil;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import com.kede.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-06-06
**/
@Service
@RequiredArgsConstructor
public class ZhiUserCertifiedServiceImpl implements ZhiUserCertifiedService {

    private final ZhiUserCertifiedRepository zhiUserCertifiedRepository;
    private final ZhiUserCertifiedMapper zhiUserCertifiedMapper;
    private final SuppliesStoreRepository suppliesStoreRepository;

    @Override
    public Map<String,Object> queryAll(ZhiUserCertifiedQueryCriteria criteria, Pageable pageable){
        Page<ZhiUserCertified> page = zhiUserCertifiedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        page.getContent().forEach(zhiUserCertified -> {
            List<SuppliesStore> allByStoreId = suppliesStoreRepository.findAllByStoreId(zhiUserCertified.getUserId());
            if (CollectionUtil.isNotEmpty(allByStoreId)) {
                zhiUserCertified.setSuppliesStoreList(allByStoreId);
            }
        });
        return PageUtil.toPage(page.map(zhiUserCertifiedMapper::toDto));
    }

    @Override
    public List<ZhiUserCertifiedDto> queryAll(ZhiUserCertifiedQueryCriteria criteria){
        return zhiUserCertifiedMapper.toDto(zhiUserCertifiedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public ZhiUserCertifiedDto findById(Integer id) {
        ZhiUserCertified zhiUserCertified = zhiUserCertifiedRepository.findById(id).orElseGet(ZhiUserCertified::new);
        ValidationUtil.isNull(zhiUserCertified.getId(),"ZhiUserCertified","id",id);
        return zhiUserCertifiedMapper.toDto(zhiUserCertified);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhiUserCertifiedDto create(ZhiUserCertified resources) {
        return zhiUserCertifiedMapper.toDto(zhiUserCertifiedRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhiUserCertified resources) {
        ZhiUserCertified zhiUserCertified = zhiUserCertifiedRepository.findById(resources.getId()).orElseGet(ZhiUserCertified::new);
        ValidationUtil.isNull( zhiUserCertified.getId(),"ZhiUserCertified","id",resources.getId());
        zhiUserCertified.copy(resources);
        zhiUserCertifiedRepository.save(zhiUserCertified);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            zhiUserCertifiedRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhiUserCertifiedDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhiUserCertifiedDto zhiUserCertified : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", zhiUserCertified.getUserId());
            map.put("0=未支付,1=已支付", zhiUserCertified.getIsPay());
            map.put("门店名称", zhiUserCertified.getStoreName());
            map.put("姓名", zhiUserCertified.getUserName());
            map.put("门店头像", zhiUserCertified.getAvatarimage());
            map.put("联系电话", zhiUserCertified.getMobile());
            map.put("居住地址", zhiUserCertified.getAddress());
            map.put("详细地址", zhiUserCertified.getAddressDetaail());
            map.put("身份证号", zhiUserCertified.getCardNo());
            map.put("身份证人像", zhiUserCertified.getHeadimage());
            map.put("身份证国徽", zhiUserCertified.getBgimage());
            map.put("营业执照", zhiUserCertified.getZhizhaoimage());
            map.put("1营业中0休息中", zhiUserCertified.getStoreStatus());
            map.put("经度", zhiUserCertified.getLng());
            map.put("维度", zhiUserCertified.getLat());
            map.put("1门店2区代理3市代理", zhiUserCertified.getType());
            map.put("状态0审核中1审核通过2审核拒绝", zhiUserCertified.getStatus());
            map.put("拒绝原因", zhiUserCertified.getReason());
            map.put("营业状态1营业中0歇业", zhiUserCertified.getIsWork());
            map.put("订单编号", zhiUserCertified.getOrderSn());
            map.put("认证金额", zhiUserCertified.getMoney());
            map.put("营业开始时间", zhiUserCertified.getStarttime());
            map.put("营业结束时间", zhiUserCertified.getEndtime());
            map.put("时间", zhiUserCertified.getCreatetime());
            map.put("时间", zhiUserCertified.getUpdatetime());
            map.put(" sheng",  zhiUserCertified.getSheng());
            map.put(" shi",  zhiUserCertified.getShi());
            map.put(" qu",  zhiUserCertified.getQu());
            map.put("区级地区编码", zhiUserCertified.getAreaCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}